NASM := nasm
CC	 := gcc
LD	 := ld
IPL_BIM := IPL.bin
START_HEAD_BIN := StartHead.bin
SYSOS_IMG := ../MagicQpOS.img
SYSOS_NAME:= MagicQpOS

KERNEL_DIR := kernel

CFLAGS := -march=i486 -m32
CFLAGS += -Wall -Wno-format -Wno-unused
CFLAGS += -std=gnu99 -static -g -O0
CFLAGS += -ffunction-sections -nostdlib -nostdinc -ffreestanding
CFLAGS += -fno-pie -fno-pic  -fno-builtin

OS_DIR_NUM=3
OS_DIR := $(shell find $(.) -maxdepth $(OS_DIR_NUM) -type d)
CHECK_FILE = $(shell if [ -f "MagicQpOS.img" ]; then echo "yes"; else echo "no"; fi)
ERR_TIPS = "${SYSOS_NAME}.img 文件不存在,需要帮助,请运行 make help"

export NASM CC LD IPL_BIM START_HEAD_BIN SYSOS_IMG SYSOS_NAME CFLAGS

all: ECHO default END_ECHO


ECHO:
	@echo
	@echo start compile kernel ....
	@echo


default:
	@for dir in ${OS_DIR}; do \
	if [ $$dir != "." ]; then make -C $$dir; fi;\
	done


END_ECHO:
	@echo
	@echo  \##########################################################
	@echo
	@echo  \    end compiled kernel ....
	@echo
	@echo \##########################################################
	@echo

clean:
	@for dir in ${OS_DIR}; do \
	if [ $$dir != "." ]; then make clean -C $$dir; fi;\
	done

mkimg:
ifeq ("$(CHECK_FILE)", "yes")
	make mkimg -C $(KERNEL_DIR)
else
	@echo $(ERR_TIPS)
endif


create_img:
	make create_img -C $(KERNEL_DIR)

run:
ifeq ("$(CHECK_FILE)", "yes")
	make run -C $(KERNEL_DIR)
else
	@echo $(ERR_TIPS)
endif


move:
ifeq ("$(CHECK_FILE)", "yes")
	make move -C $(KERNEL_DIR)
else
	@echo $(ERR_TIPS)
endif

delete_img:
ifeq ("$(CHECK_FILE)", "yes")
	make delete_img -C $(KERNEL_DIR)
else
	@echo $(ERR_TIPS)
endif

help:
	@echo " \033[32m create_img:\033[0m 生成img文件"
	@echo " \033[32m mkimg:     \033[0m 将系统制作成img文件"
	@echo " \033[32m run:       \033[0m 通过qemu运行镜像(qemu-system-x86_64 -fda $(SYSOS_IMG))"
	@echo " \033[32m clean:     \033[0m 清除编译文件"
	@echo " \033[32m delete_img:\033[0m 删除镜像文件"
# @echo " \033[5m \033[42;37m help \033[0m"